#delimit;

clear _all;
cd/feryal;
set memory 60m;
use hr_June23.dta ;
gen yrintv=.;
sort ccode year;
merge ccode year using fortna_UN_peacekeeping;
replace unpeace=0 if unpeace==.;
drop _merge;
sort ccode year thrdpar yrintv;
merge ccode year thrdpar yrintv using c:/feryal/regan2 , update;
drop _merge;

sort ccode year thrdpar;
by ccode year:replace thrdpar=. if thrdpar-thrdpar[_n-1]==0;

sort ccode year ;
replace xconst2=xconst2/7;
replace xropen2=(xropen2+1)/5;
replace xrcomp2=(xrcomp2+1)/4;
replace parreg2=parreg2/5;
replace parcomp2=(parcomp2+1)/6;
keep if year~=. & year>1945;

sort ccode year;
by ccode: gen x=year-year[_n-1];
sort ccode year x;
by ccode year: gen newx=_n if x==0;
/*replace newx=newx*/;
/*replace x=newx if x==0 & newx~=0*/;
gen ccodeyr=ccode*year if x==0;
sort ccode year;
egen maxnewx=max(newx) if ccodeyr~=., by(ccode year);
replace newx=0 if newx==. & maxnewx~=.;

sort ccode year;
replace AUT=((-1*AUT)+2)/2 /* 1 =Democracy .5 = bureaucracy 0 = authoritarian ACLP data*/;
gen civilwar=CL_WAR;
replace hostlev=. if initiate==1;
gen othercase=0;
replace othercase=1 if hostlev~=.&hostlev>1;
replace civilwar=0 if CL_WAR==.;
replace civilwar=1 if ((civilwar==0|civilwar==.)&othercase==1)|(unpeace>0&unpeace~=.);
replace civilwar=0 if civilwar==0&othercase==0&unpeace==0;
gen milyn=0;

replace yrintv=yrintv+1900;
replace yrend=yrend+1900;

by ccode: replace success=0 if success~=1&year~=.;
by ccode: replace success=sum(success);
by ccode: replace success=1 if success>0&success~=.;


by ccode: replace success=0 if success==1 & yrintv>year;

replace milyn=1 if (((type==10|type==40)|(hostlev~=.&hostlev>1)|
(unpeace>0&unpeace~=.)))&year>=yrintv;
by ccode:replace milyn=sum(milyn);
by ccode: replace milyn=1 if milyn>0&milyn~=.;
by ccode: replace success=0 if milyn==0;

gen civilmil=civilwar*milyn;
gen end=yrend if milyn==1;

gen usa=0;
replace usa=1 if (thrdpar==2|(ccode==2&hostlev~=.&hostlev>1))  & milyn==1;
by ccode: replace usa=sum(usa);
by ccode: replace usa=1 if usa>0&usa~=.;

gen interven2=0;
replace interven2=1 if yrintv>0 & yrintv~=.;

/*replace interven2=0 if interven==. & year>1947 & year~=.*/;

gen france=0;
replace france=1 if (thrdpar==220 & milyn==1);
by ccode: replace france=sum(france);
by ccode: replace france=1 if france>0&france~=.;
gen russia=0;
replace russia=1 if (thrdpar==365 & milyn==1);
by ccode: replace russia=sum(russia);
by ccode: replace russia=1 if russia>0&russia~=.;
gen un=0;
replace  un=1 if ((thrdpar==1)|(thrdpar==1&hostlev~=.&hostlev>1)|
(unpeace~=.&unpeace>0))&milyn==1;

by ccode: replace un=sum(un);
by ccode: replace un=1 if un>0 & un~=.;
/*  NOTE demmil is intervention by democratic third party including USA */;
gen demmil=0;
replace demmil=1 if milyn==1&((thrdpar==2|thrdpar==200|thrdpar==220|thrdpar==211|thrdpar==255|
thrdpar==385|thrdpar==900|thrdpar==920|thrdpar==640|thrdpar==750));
replace demmil=1 if hostlev~=.&hostlev>1&((ccode==2|ccode==200|ccode==220|ccode==211|ccode==255|
ccode==385|ccode==900|ccode==920|ccode==640|ccode==750));
by ccode: replace demmil=sum(demmil);
by ccode: replace demmil=1 if demmil>0 & demmil~=.;
replace type=0 if type==.&year~=.;

gen aut=0;
replace aut=1 if milyn==1&
(thrdpar~=.&thrdpar~=1&thrdpar~=2&thrdpar~=20&(thrdpar>20&thrdpar<200))|
(thrdpar>=365&thrdpar~=385&thrdpar~=900&thrdpar~=640&thrdpar~=750);
replace aut=1 if hostlev~=.&hostlev>1&
(ccode~=.&ccode~=1&ccode~=2&ccode~=20&(ccode>20&ccode<200))|
(ccode>=365&ccode~=385&ccode~=900&ccode~=640&ccode~=750);


replace aut=0 if aut==.&year~=.;

sort ccode year;

gen usasuccess=usa*success;
gen unsuccess=un*success;
gen demsuccess=demmil*succes;

by ccode: gen militar=sum(interven2);
sort ccode year militar;
by ccode: gen stopyear=year[_n+10] if interven2==1 ;
by ccode: gen mnstopyr=stopyear if milyn==1&militar==1;
by ccode: replace mnstopyr=stopyear if milyn==1&militar==2;
by ccode: replace mnstopyr=stopyear if milyn==1&militar==3;
by ccode: replace mnstopyr=stopyear if milyn==1&militar==4;


sort ccode year militar;

egen xyz=max(mnstopyr) , by(ccode militar);

by ccode: gen F10demaut=demaut[_n+10] ;
by ccode: gen F10W=W[_n+10] ;
by ccode: gen F10AUT=AUT[_n+10];
by ccode: gen F10parcomp2=parcomp2[_n+10];

sort ccode year newx;
by ccode : replace F10demaut=demaut[_n+15] if newx==1 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F10demaut=demaut[_n+14] if newx==2 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F10demaut=demaut[_n+13] if newx==3 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F10demaut=demaut[_n+12] if newx==4 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F10demaut=demaut[_n+11] if newx==5 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F10demaut=demaut[_n+14] if newx==1 &maxnewx==4 & ccodeyr~=.;
by ccode : replace F10demaut=demaut[_n+13] if newx==2 &maxnewx==4 & ccodeyr~=.;
by ccode : replace F10demaut=demaut[_n+12] if newx==3 &maxnewx==4 & ccodeyr~=.;
by ccode : replace F10demaut=demaut[_n+11] if newx==4 &maxnewx==4 & ccodeyr~=.;
by ccode : replace F10demaut=demaut[_n+13] if newx==1 &maxnewx==3 & ccodeyr~=.;
by ccode : replace F10demaut=demaut[_n+12] if newx==2 &maxnewx==3 & ccodeyr~=.;
by ccode : replace F10demaut=demaut[_n+11] if newx==3 &maxnewx==3 & ccodeyr~=.;
by ccode : replace F10demaut=demaut[_n+12] if newx==1 &maxnewx==2 & ccodeyr~=.;
by ccode : replace F10demaut=demaut[_n+11] if newx==2 &maxnewx==2 & ccodeyr~=.;
by ccode : replace F10demaut=demaut[_n+11] if newx==1 &maxnewx==1 & ccodeyr~=.;
by ccode : replace F10W=W[_n+15] if newx==1 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F10W=W[_n+14] if newx==2 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F10W=W[_n+13] if newx==3 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F10W=W[_n+12] if newx==4 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F10W=W[_n+11] if newx==5 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F10W=W[_n+14] if newx==1 &maxnewx==4 & ccodeyr~=.;
by ccode : replace F10W=W[_n+13] if newx==2 &maxnewx==4 & ccodeyr~=.;
by ccode : replace F10W=W[_n+12] if newx==3 &maxnewx==4 & ccodeyr~=.;
by ccode : replace F10W=W[_n+11] if newx==4 &maxnewx==4 & ccodeyr~=.;
by ccode : replace F10W=W[_n+13] if newx==1 &maxnewx==3 & ccodeyr~=.;
by ccode : replace F10W=W[_n+12] if newx==2 &maxnewx==3 & ccodeyr~=.;
by ccode : replace F10W=W[_n+11] if newx==3 &maxnewx==3 & ccodeyr~=.;
by ccode : replace F10W=W[_n+12] if newx==1 &maxnewx==2 & ccodeyr~=.;
by ccode : replace F10W=W[_n+11] if newx==2 &maxnewx==2 & ccodeyr~=.;
by ccode : replace F10W=W[_n+11] if newx==1 &maxnewx==1 & ccodeyr~=.;
by ccode : replace F10AUT=AUT[_n+15] if newx==1 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F10AUT=AUT[_n+14] if newx==2 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F10AUT=AUT[_n+13] if newx==3 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F10AUT=AUT[_n+12] if newx==4 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F10AUT=AUT[_n+11] if newx==5 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F10AUT=AUT[_n+14] if newx==1 &maxnewx==4 & ccodeyr~=.;
by ccode : replace F10AUT=AUT[_n+13] if newx==2 &maxnewx==4 & ccodeyr~=.;
by ccode : replace F10AUT=AUT[_n+12] if newx==3 &maxnewx==4 & ccodeyr~=.;
by ccode : replace F10AUT=AUT[_n+11] if newx==4 &maxnewx==4 & ccodeyr~=.;
by ccode : replace F10AUT=AUT[_n+13] if newx==1 &maxnewx==3 & ccodeyr~=.;
by ccode : replace F10AUT=AUT[_n+12] if newx==2 &maxnewx==3 & ccodeyr~=.;
by ccode : replace F10AUT=AUT[_n+11] if newx==3 &maxnewx==3 & ccodeyr~=.;
by ccode : replace F10AUT=AUT[_n+12] if newx==1 &maxnewx==2 & ccodeyr~=.;
by ccode : replace F10AUT=AUT[_n+11] if newx==2 &maxnewx==2 & ccodeyr~=.;
by ccode : replace F10AUT=AUT[_n+11] if newx==1 &maxnewx==1 & ccodeyr~=.;
by ccode : replace F10parcomp2=parcomp2[_n+15] if newx==1 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F10parcomp2=parcomp2[_n+14] if newx==2 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F10parcomp2=parcomp2[_n+13] if newx==3 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F10parcomp2=parcomp2[_n+12] if newx==4 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F10parcomp2=parcomp2[_n+11] if newx==5 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F10parcomp2=parcomp2[_n+14] if newx==1 &maxnewx==4 & ccodeyr~=.;
by ccode : replace F10parcomp2=parcomp2[_n+13] if newx==2 &maxnewx==4 & ccodeyr~=.;
by ccode : replace F10parcomp2=parcomp2[_n+12] if newx==3 &maxnewx==4 & ccodeyr~=.;
by ccode : replace F10parcomp2=parcomp2[_n+11] if newx==4 &maxnewx==4 & ccodeyr~=.;
by ccode : replace F10parcomp2=parcomp2[_n+13] if newx==1 &maxnewx==3 & ccodeyr~=.;
by ccode : replace F10parcomp2=parcomp2[_n+12] if newx==2 &maxnewx==3 & ccodeyr~=.;
by ccode : replace F10parcomp2=parcomp2[_n+11] if newx==3 &maxnewx==3 & ccodeyr~=.;
by ccode : replace F10parcomp2=parcomp2[_n+12] if newx==1 &maxnewx==2 & ccodeyr~=.;
by ccode : replace F10parcomp2=parcomp2[_n+11] if newx==2 &maxnewx==2 & ccodeyr~=.;
by ccode : replace F10parcomp2=parcomp2[_n+11] if newx==1 &maxnewx==1 & ccodeyr~=.;

by ccode: gen F5demaut=demaut[_n+5];
by ccode: gen F5W=W[_n+5];
by ccode: gen F5AUT=AUT[_n+5];
by ccode: gen F5parcomp2=parcomp2[_n+5];
sort ccode year newx;
by ccode : replace F5demaut=demaut[_n+10] if newx==1 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F5demaut=demaut[_n+9] if newx==2 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F5demaut=demaut[_n+8] if newx==3 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F5demaut=demaut[_n+7] if newx==4 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F5demaut=demaut[_n+6] if newx==5 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F5demaut=demaut[_n+9] if newx==1 &maxnewx==4 & ccodeyr~=.;
by ccode : replace F5demaut=demaut[_n+8] if newx==2 &maxnewx==4 & ccodeyr~=.;
by ccode : replace F5demaut=demaut[_n+7] if newx==3 &maxnewx==4 & ccodeyr~=.;
by ccode : replace F5demaut=demaut[_n+6] if newx==4 &maxnewx==4 & ccodeyr~=.;
by ccode : replace F5demaut=demaut[_n+8] if newx==1 &maxnewx==3 & ccodeyr~=.;
by ccode : replace F5demaut=demaut[_n+7] if newx==2 &maxnewx==3 & ccodeyr~=.;
by ccode : replace F5demaut=demaut[_n+6] if newx==3 &maxnewx==3 & ccodeyr~=.;
by ccode : replace F5demaut=demaut[_n+7] if newx==1 &maxnewx==2 & ccodeyr~=.;
by ccode : replace F5demaut=demaut[_n+6] if newx==2 &maxnewx==2 & ccodeyr~=.;
by ccode : replace F5demaut=demaut[_n+6] if newx==1 &maxnewx==1 & ccodeyr~=.;
by ccode : replace F5W=W[_n+10] if newx==1 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F5W=W[_n+9] if newx==2 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F5W=W[_n+8] if newx==3 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F5W=W[_n+7] if newx==4 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F5W=W[_n+6] if newx==5 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F5W=W[_n+9] if newx==1 &maxnewx==4 & ccodeyr~=.;
by ccode : replace F5W=W[_n+8] if newx==2 &maxnewx==4 & ccodeyr~=.;
by ccode : replace F5W=W[_n+7] if newx==3 &maxnewx==4 & ccodeyr~=.;
by ccode : replace F5W=W[_n+6] if newx==4 &maxnewx==4 & ccodeyr~=.;
by ccode : replace F5W=W[_n+8] if newx==1 &maxnewx==3 & ccodeyr~=.;
by ccode : replace F5W=W[_n+7] if newx==2 &maxnewx==3 & ccodeyr~=.;
by ccode : replace F5W=W[_n+6] if newx==3 &maxnewx==3 & ccodeyr~=.;
by ccode : replace F5W=W[_n+7] if newx==1 &maxnewx==2 & ccodeyr~=.;
by ccode : replace F5W=W[_n+6] if newx==2 &maxnewx==2 & ccodeyr~=.;
by ccode : replace F5W=W[_n+6] if newx==1 &maxnewx==1 & ccodeyr~=.;
by ccode : replace F5AUT=AUT[_n+10] if newx==1 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F5AUT=AUT[_n+9] if newx==2 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F5AUT=AUT[_n+8] if newx==3 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F5AUT=AUT[_n+7] if newx==4 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F5AUT=AUT[_n+6] if newx==5 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F5AUT=AUT[_n+9] if newx==1 &maxnewx==4 & ccodeyr~=.;
by ccode : replace F5AUT=AUT[_n+8] if newx==2 &maxnewx==4 & ccodeyr~=.;
by ccode : replace F5AUT=AUT[_n+7] if newx==3 &maxnewx==4 & ccodeyr~=.;
by ccode : replace F5AUT=AUT[_n+6] if newx==4 &maxnewx==4 & ccodeyr~=.;
by ccode : replace F5AUT=AUT[_n+8] if newx==1 &maxnewx==3 & ccodeyr~=.;
by ccode : replace F5AUT=AUT[_n+7] if newx==2 &maxnewx==3 & ccodeyr~=.;
by ccode : replace F5AUT=AUT[_n+6] if newx==3 &maxnewx==3 & ccodeyr~=.;
by ccode : replace F5AUT=AUT[_n+7] if newx==1 &maxnewx==2 & ccodeyr~=.;
by ccode : replace F5AUT=AUT[_n+6] if newx==2 &maxnewx==2 & ccodeyr~=.;
by ccode : replace F5AUT=AUT[_n+6] if newx==1 &maxnewx==1 & ccodeyr~=.;
by ccode : replace F5parcomp2=parcomp2[_n+10] if newx==1 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F5parcomp2=parcomp2[_n+9] if newx==2 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F5parcomp2=parcomp2[_n+8] if newx==3 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F5parcomp2=parcomp2[_n+7] if newx==4 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F5parcomp2=parcomp2[_n+6] if newx==5 &maxnewx==5 & ccodeyr~=.;
by ccode : replace F5parcomp2=parcomp2[_n+9] if newx==1 &maxnewx==4 & ccodeyr~=.;
by ccode : replace F5parcomp2=parcomp2[_n+8] if newx==2 &maxnewx==4 & ccodeyr~=.;
by ccode : replace F5parcomp2=parcomp2[_n+7] if newx==3 &maxnewx==4 & ccodeyr~=.;
by ccode : replace F5parcomp2=parcomp2[_n+6] if newx==4 &maxnewx==4 & ccodeyr~=.;
by ccode : replace F5parcomp2=parcomp2[_n+8] if newx==1 &maxnewx==3 & ccodeyr~=.;
by ccode : replace F5parcomp2=parcomp2[_n+7] if newx==2 &maxnewx==3 & ccodeyr~=.;
by ccode : replace F5parcomp2=parcomp2[_n+6] if newx==3 &maxnewx==3 & ccodeyr~=.;
by ccode : replace F5parcomp2=parcomp2[_n+7] if newx==1 &maxnewx==2 & ccodeyr~=.;
by ccode : replace F5parcomp2=parcomp2[_n+6] if newx==2 &maxnewx==2 & ccodeyr~=.;
by ccode : replace F5parcomp2=parcomp2[_n+6] if newx==1 &maxnewx==1 & ccodeyr~=.;



gen demautmil=demaut*milyn;
gen Wmil=W*milyn;
gen Smil=s*milyn;
gen AUTmil=AUT*milyn;
gen parcomp2mil=parcomp2*milyn;
gen d10dem=F10demaut-demaut;
gen d10W=F10W-W;
gen d10parcomp2=F10parcomp2-parcomp2;
gen d5dem=F5demaut-demaut;
gen d5W=F5W-W;
gen d5parcomp2=F5parcomp2-parcomp2;

gen logenergy=log(energy+1);
gen logirst=log(irst+1);
gen lmilex=log(milex+1);
gen lmilper=log(milper+1);


log using intervention_effect_select_USDem1.log, replace;

heckman lgdpWB logenergy logirst lmilex lmilper year, select(W S);
predict income, ycond;
gen incomemil=income*milyn;

keep if (usa~=.|un~=.|demmil~=.|aut~=.|thrdpar~=.)|(milyn==0)|(interven2==0)|(hostlev~=.);
drop if thrdpar==.&(interven2==1);
sort ccode year;
egen CL_WAR_running=sum(civilwar);
gen interventiondur=yrend-(yrstrt-1900);

/*keep if year<=xyz*/;
regr interventiondur milyn un demmil usa if interven2==1;
regr interventiondur milyn rus un demmil usa if interven2==1;


 xtreg d10dem milyn usa un demmil year  , fe i(ccode);
test usa+demmil=un;
test demmil=un; test demmil=usa;

xtreg d10W milyn usa un demmil year  , fe i(ccode);
test year=0;
test year=year+milyn;
test year=year+milyn+usa+demmil;
test year=year+milyn+demmil;
test year=year+milyn+un;
test year=year+milyn+usa+un+demmil;
test usa+demmil=un;
test un=demmil; test demmil=usa;

xtreg d10parcomp2 milyn usa un demmil year  , fe i(ccode);
test year=0;
test year=year+milyn;
test year=year+milyn+usa+demmil;
test year=year+milyn+un;
test year=year+milyn+demmil;
test year=year+milyn+usa+un+demmil;
test demmil=usa;

test usa+demmil=un;
test un=demmil;

 xtreg d10dem demaut milyn usa un demmil year  , fe i(ccode);
test demaut+year=demaut+year+milyn+usa+un+demmil;

test usa+demmil=un;
test demmil=un;
test demmil=usa;

xtreg d10W W milyn usa un demmil year  , fe i(ccode);
test year=0;
test year=year+milyn;
test year=year+milyn+usa+demmil;
test year=year+milyn+demmil;
test year=year+milyn+un;
test year=year+milyn+usa+un+demmil;
test usa+demmil=un;
test un=demmil;
test demmil=usa; 

xtreg d10parcomp2 parcomp2 milyn usa un demmil year  , fe i(ccode);
test year=0;
test year=year+milyn;
test year=year+milyn+usa+demmil;
test year=year+milyn+un;
test year=year+milyn+demmil;
test year=year+milyn+usa+un+demmil;

test usa+demmil=un;
test un=demmil;
test demmil=usa;

xtreg F10demaut  demaut milyn un usa demmil year  , fe i(ccode);
predict F10demEurope if demmil==1 & usa==0;
predict F10demAmerica if usa==1 & demmil==0;
predict F10demEuropenoUN if demmil==1&usa==0&un==0;
predict F10demAmericanoUN if usa==1 & un==0 & demmil==0;


test demaut+year=0;
test demaut+year=demaut+year+milyn;
test demaut+year=demaut+year+milyn+demmil;
test demaut+year=demaut+year+milyn+usa+demmil;
test demaut+year=demaut+year+milyn+un;
test demaut+year=demaut+year+milyn+un+demmil;
test demaut+year=demaut+year+milyn+un+usa+demmil;
test demmil=usa;
test (year*10)=(year*10)+milyn;
test (year*10)=(year*10)+milyn+un;
test (year*10)=(year*10)+milyn+demmil;
test (year*10)=(year*10)+milyn+usa+demmil;
test (year*10)=(year*10)+milyn+un+demmil;
test (year*10)=(year*10)+milyn+un+demmil+usa;



xtreg F10W W milyn un usa demmil year  , fe i(ccode);
predict F10WEurope if demmil==1 & usa==0;
predict F10WAmerica if usa==1 & demmil==0;
predict F10WEuropenoUN if demmil==1&usa==0&un==0;
predict F10WAmericanoUN if usa==1 & un==0 & demmil==0;


test W+year=0;
test W+year=W+year+milyn;
test W+year=W+year+milyn+usa+demmil;
test W+year=W+year+milyn+un;
test W+year=W+year+milyn+demmil;
test W+year=W+year+milyn+un+demmil;
test W+year=W+year+milyn+un+usa+demmil;
test demmil=usa;
test (year*10)=(year*10)+milyn;
test (year*10)=(year*10)+milyn+un;
test (year*10)=(year*10)+milyn+demmil;
test (year*10)=(year*10)+milyn+usa+demmil;
test (year*10)=(year*10)+milyn+un+demmil;
test (year*10)=(year*10)+milyn+un+demmil+usa;


xtreg F10parcomp2 parcomp2 milyn un usa demmil year  , fe i(ccode);
predict F10parEurope if demmil==1 & usa==0;
predict F10parAmerica if usa==1 & demmil==0;
predict F10parEuropenoUN if demmil==1&usa==0&un==0;
predict F10parAmericanoUN if usa==1 & un==0 & demmil==0 ;


test parcomp2+year=0;
test parcomp2+year=parcomp2+year+milyn;
test parcomp2+year=parcomp2+year+milyn+demmil;
test parcomp2+year=parcomp2+year+milyn+un;
test parcomp2+year=parcomp2+year+milyn+usa+demmil;
test parcomp2+year=parcomp2+year+milyn+un+demmil;
test parcomp2+year=parcomp2+year+milyn+un+usa+demmil;

test demmil=usa;
test (year*10)=(year*10)+milyn;
test (year*10)=(year*10)+milyn+un;
test (year*10)=(year*10)+milyn+demmil;
test (year*10)=(year*10)+milyn+usa+demmil;
test (year*10)=(year*10)+milyn+un+demmil;
test (year*10)=(year*10)+milyn+un+demmil+usa;






/**************************   5 Year Tests   *************************************/;
xtreg F5demaut  demaut milyn un usa demmil year  , fe i(ccode);

test demaut+year=0;
test demaut+year=demaut+year+milyn;
test demaut+year=demaut+year+milyn+demmil;
test demaut+year=demaut+year+milyn+usa+demmil;
test demaut+year=demaut+year+milyn+un;
test demaut+year=demaut+year+milyn+un+demmil;
test demaut+year=demaut+year+milyn+un+usa+demmil;
test demmil=usa;
test (year*5)=(year*5)+milyn;
test (year*5)=(year*5)+milyn+un;
test (year*5)=(year*5)+milyn+demmil;
test (year*5)=(year*5)+milyn+usa+demmil;
test (year*5)=(year*5)+milyn+un+demmil;
test (year*5)=(year*5)+milyn+un+demmil+usa;


xtreg F5W W milyn un usa demmil year  , fe i(ccode);

test W+year=0;
test W+year=W+year+milyn;
test W+year=W+year+milyn+usa+demmil;
test W+year=W+year+milyn+un;
test W+year=W+year+milyn+demmil;
test W+year=W+year+milyn+un+demmil;
test W+year=W+year+milyn+un+usa+demmil;
test demmil=usa;
test (year*5)=(year*5)+milyn;
test (year*5)=(year*5)+milyn+un;
test (year*5)=(year*5)+milyn+demmil;
test (year*5)=(year*5)+milyn+usa+demmil;
test (year*5)=(year*5)+milyn+un+demmil;
test (year*5)=(year*5)+milyn+un+demmil+usa;

xtreg F5parcomp2 parcomp2 milyn un usa demmil year  , fe i(ccode);

test parcomp2+year=0;
test parcomp2+year=parcomp2+year+milyn;
test parcomp2+year=parcomp2+year+milyn+demmil;
test parcomp2+year=parcomp2+year+milyn+un;
test parcomp2+year=parcomp2+year+milyn+usa+demmil;
test parcomp2+year=parcomp2+year+milyn+un+demmil;
test parcomp2+year=parcomp2+year+milyn+un+usa+demmil;

test demmil=usa;
test (year*5)=(year*5)+milyn;
test (year*5)=(year*5)+milyn+un;
test (year*5)=(year*5)+milyn+demmil;
test (year*5)=(year*5)+milyn+usa+demmil;
test (year*5)=(year*5)+milyn+un+demmil;
test (year*5)=(year*5)+milyn+un+demmil+usa;














sum F10* if milyn==1;

sort ccode year;
replace demmil=0;
replace demmil=1 if milyn==1&((thrdpar==200|thrdpar==220|thrdpar==211|thrdpar==255|
thrdpar==385|thrdpar==900|thrdpar==920|thrdpar==640|thrdpar==750));
replace demmil=1 if hostlev~=.&hostlev>1&((ccode==200|ccode==220|ccode==211|ccode==255|
ccode==385|ccode==900|ccode==920|ccode==640|ccode==750));
by ccode: replace demmil=sum(demmil);
by ccode: replace demmil=1 if demmil>0 & demmil~=.;


sum demaut if usa==1 & demmil==0 & milyn==1 , detail;
sum demaut if usa==0 & demmil==1 &milyn==1, detail;

sum W if usa==1 & demmil==0 & milyn==1 , detail;
sum W if usa==0 & demmil==1 &milyn==1, detail;

sum parcomp2 if usa==1 & demmil==0 & milyn==1 , detail;
sum parcomp2 if usa==0 & demmil==1 &milyn==1, detail;

sum F10demaut if usa==1 & demmil==0 & milyn==1 , detail;
sum F10demaut if usa==0 & demmil==1 &milyn==1, detail;

sum F10W if usa==1 & demmil==0 & milyn==1 , detail;
sum F10W if usa==0 & demmil==1 &milyn==1, detail;

sum F10parcomp2 if usa==1 & demmil==0 & milyn==1 , detail;
sum F10parcomp2 if usa==0 & demmil==1 &milyn==1, detail;



sum d10dem if usa==1 & demmil==0 & milyn==1 ; sum d10dem if demmil==1 & usa==0 & milyn==1;
sum d10W if usa==1 & demmil==0 & milyn==1 ; sum d10W if demmil==1 & usa==0 & milyn==1;
sum d10par if usa==1 & demmil==0 & milyn==1; sum d10par if demmil==1 & usa==0 & milyn==1;

xtreg d10dem milyn usa demmil year if demaut>=.7 & un==0 & demaut~=., fe i(ccode);
predict chdem7dem if demmil==1 & usa==0 & milyn==1;
predict chdem7usa if demmil==0 & usa==1 & milyn==1;

xtreg d10dem milyn usa demmil year if demaut<=.35 & un==0 & demaut~=., fe i(ccode);
predict chdem35dem if demmil==1 & usa==0 & milyn==1;
predict chdem35usa if demmil==0 & usa==1 & milyn==1;

xtreg d10dem milyn usa demmil year if demaut<.7 & demaut>.35 & un==0 & demaut~=., fe i(ccode);
predict chdemmiddem if demmil==1 & usa==0 & milyn==1;
predict chdemmidusa if demmil==0 & usa==1 & milyn==1;

gen USA=0; replace USA=chdem35usa if demaut<=.35; 
replace USA=chdem7usa if demaut>=.7 & demaut~=.; 
replace USA=chdemmidusa if demaut>.35&demaut<.7;

gen Democrat=0; replace Democrat=chdem35dem if demaut<=.35; 
replace Democrat=chdem7dem if demaut>=.7 & demaut~=.; 
replace Democrat=chdemmiddem if demaut>.35&demaut<.7;

graph bar USA Democrat, over(demaut);
log close;


